﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JYH.Data;
using JYH.Framework;

namespace JYH.DataAccess
{
    public class TableColumnInfoBLL : BusinessBase<TableColumnInfo>
    {
        public TableColumnInfoBLL()
        {


            this.SqlSelect = @"Select * From(
                                SELECT
                                 a.id TableID,
                                 (case when a.colorder=1 then d.name else '' end) TableName,
                                  d.name TableKey,
                                 a.colorder ColumnIndex,
                                 a.name ColumnName,
                                 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '0' end) ColIsIdentity,
                                 (case when (SELECT count(*)
                                 FROM sysobjects
                                 WHERE (name in
                                 (SELECT name
                                 FROM sysindexes
                                 WHERE (id = a.id) AND (indid in
                                 (SELECT indid
                                 FROM sysindexkeys
                                 WHERE (id = a.id) AND (colid in
                                 (SELECT colid
                                 FROM syscolumns
                                 WHERE (id = a.id) AND (name = a.name))))))) AND
                                 (xtype = 'PK'))>0 then '1' else '0' end) MainKey,
                                 b.name ColType,
                                 COLUMNPROPERTY(a.id,a.name,'PRECISION') as ColLength,
                                 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as DecimalQty,
                                 (case when a.isnullable=1 then '1'else '0' end) ColIsNull,
                                 isnull(e.text,'') DefaultValue,
                                 isnull(g.[value],'') AS ColDesc 
                                FROM syscolumns a left join systypes b
                                on a.xtype=b.xusertype
                                inner join sysobjects d
                                on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
                                left join syscomments e 
                                on a.cdefault=e.id
                                left join sys.extended_properties g
                                on a.id=g.major_id AND a.colid = g.minor_id
                                ) TableColumnInfo
";
            myDataBase = DB.D(true);
        }

        public  List<TableColumnInfo> GetTableColumn(string TableName, out string Msg)
        {
            List<TableColumnInfo> list = new List<TableColumnInfo>();
            Msg = string.Empty;
            Sql sql = new Sql();
            sql.Append(SqlSelect);
            if (!string.IsNullOrEmpty(TableName))
            {
                sql.Append("Where TableColumnInfo.TableKey Like '%" + TableName + "%'");
            }

            sql.OrderBy("TableColumnInfo.TableKey ,TableColumnInfo.ColumnIndex");
            list= myDataBase.Fetch<TableColumnInfo>(sql);
            return list;

            
        }
        public bool Login(string UserNO,string Pwd,out string Msg)
        {
            Msg = string.Empty;
            bool res = true;
            if (!string.Equals(UserNO.ToUpper(), "Admin".ToUpper()))
            {
                Msg = "用户帐号不正确";
                return false;
            }
            string StrPwd = "";
            if (ConfigHelper.CheckAppSettingKey("SYSPWD"))
            {
                StrPwd = ConfigHelper.GetValue("SYSPWD");
            }
            else
            {
                StrPwd = "";
            }
            if (!string.Equals(Pwd, Security.DESDecrypt(StrPwd)))
            {
                Msg = "密码不对";
                return false;
            }
            return res;
        }
    }
}
